//
//  UIImage+Extension.swift
//  CFZWeiBo
//
//  Created by Cedric Zheng on 02/12/2016.
//  Copyright © 2016 Cedric Zheng. All rights reserved.
//

import UIKit

extension UIImage {
    
    func wb_asyncDrawImage(isCornered: Bool = true, size: CGSize = CGSize(width: 100, height: 100), backgroundColor: UIColor = UIColor.white, callback: @escaping (_ image: UIImage)->()) {
        
        DispatchQueue.global().async {
            
            let rect = CGRect(origin: CGPoint.zero, size: size)
            UIGraphicsBeginImageContext(size)
            backgroundColor.setFill()
            UIRectFill(rect)
            let path = UIBezierPath(ovalIn: rect)
            path.addClip()
            self.draw(in:rect)
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            
            DispatchQueue.main.async {
                callback(image!)
            }
            
        }
        
    }
    
}


